﻿using System;
using System.Collections;
using System.Collections.Generic;
using TrueSync;
using TrueSync.Physics2D;
using Framework;

public class LogicPhysicsComponent : LogicComponent
{
    #region 参数
    public TSVector2 BodyPositionRectLeftup, BodyPositionRectRightdown;
    public TSVector2 BodyHitUpRectLeftup, BodyHitUpRectRightdown;
    public TSVector2 BodyHitMidRectLeftup, BodyHitMidRectRightdown;
    public TSVector2 BodyHitDownRectLeftup, BodyHitDownRectRightdown;
    #endregion

    LogicEntity mLEntity;

    public Fixture BodyPositionRect;//占位框
    public BodyHitRect BodyHitRectUp;//体位框,受到判定检测用,这个可以通过变层变成飞行道具无敌框
    public BodyHitRect BodyHitRectMid;
    public BodyHitRect BodyHitRectDown;
    public List<BodyHitRect> DynamicsBodyHitRects;//攻击体位框，跟体位框功能一样只是这个是Event动态创建删掉的
    public List<GuardAlarmRect> GuardAlarmRects;

    public void Init(LogicEntity LEntity)
    {
        mLEntity = LEntity;

        DynamicsBodyHitRects = new List<BodyHitRect>();
        GuardAlarmRects = new List<GuardAlarmRect>();

        BodyPositionRect = new Fixture(new RectangleShape(BodyPositionRectLeftup, BodyPositionRectRightdown), TSVector2.zero, 0, mLEntity.self);
        BodyPositionRect.CollisionCategories = Category.BodyPosition;
        FightGame.Instance.CollisionWorld.AddFixture(BodyPositionRect);

        BodyHitRectUp = new BodyHitRect(new RectangleShape(BodyHitUpRectLeftup, BodyHitUpRectRightdown), TSVector2.zero, 0, mLEntity.self);
        BodyHitRectUp.BodyHitRectType = BodyHitRectTypeEnum.Up;
        FightGame.Instance.CollisionWorld.AddFixture(BodyHitRectUp);

        BodyHitRectMid = new BodyHitRect(new RectangleShape(BodyHitMidRectLeftup, BodyHitMidRectRightdown), TSVector2.zero, 0, mLEntity.self);
        BodyHitRectMid.BodyHitRectType = BodyHitRectTypeEnum.Middle;
        FightGame.Instance.CollisionWorld.AddFixture(BodyHitRectMid);

        BodyHitRectDown = new BodyHitRect(new RectangleShape(BodyHitDownRectLeftup, BodyHitDownRectRightdown), TSVector2.zero, 0, mLEntity.self);
        BodyHitRectDown.BodyHitRectType = BodyHitRectTypeEnum.Down;
        FightGame.Instance.CollisionWorld.AddFixture(BodyHitRectDown);
    }

    public void Start()
    {
        LogicAnimationComponent animationComponent = mLEntity.GetLogicComponent<LogicAnimationComponent>();
        animationComponent.AddToHangpoint("", BodyPositionRect, AttachToParentSyncMethodEnum.SyncPosition);
        animationComponent.AddToHangpoint("", BodyHitRectUp, AttachToParentSyncMethodEnum.SyncPosition);
        animationComponent.AddToHangpoint("", BodyHitRectMid, AttachToParentSyncMethodEnum.SyncPosition);
        animationComponent.AddToHangpoint("", BodyHitRectDown, AttachToParentSyncMethodEnum.SyncPosition);
    }

    public void DoUpdate(FP deltaTime)
    {
    }

    public void OnSkillStreamFinish()
    {
        LogicAnimationComponent animationComponent = mLEntity.GetLogicComponent<LogicAnimationComponent>();
        for(int i = 0; i < DynamicsBodyHitRects.Count; i++)
        {
            animationComponent.RemoveFromHangpoint(DynamicsBodyHitRects[i]);
            DynamicsBodyHitRects[i].OnDestroy();
        }

        for (int i = 0; i < GuardAlarmRects.Count; i++)
        {
            animationComponent.RemoveFromHangpoint(GuardAlarmRects[i]);
            GuardAlarmRects[i].OnDestroy();
        }
    }

    public void Destroy()
    {
        LogicAnimationComponent animationComponent = mLEntity.GetLogicComponent<LogicAnimationComponent>();

        FightGame.Instance.CollisionWorld.RemoveFixture(BodyPositionRect);
        animationComponent.RemoveFromHangpoint(BodyPositionRect);

        FightGame.Instance.CollisionWorld.RemoveFixture(BodyHitRectUp);
        animationComponent.RemoveFromHangpoint(BodyHitRectUp);

        FightGame.Instance.CollisionWorld.RemoveFixture(BodyHitRectMid);
        animationComponent.RemoveFromHangpoint(BodyHitRectMid);

        FightGame.Instance.CollisionWorld.RemoveFixture(BodyHitRectDown);
        animationComponent.RemoveFromHangpoint(BodyHitRectDown);

        OnSkillStreamFinish();
    }
}
